package com.yd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yd.pojo.AAdmins;
import com.yd.service.IUsersService;
import com.yd.util.ConstateFinalUtil;
import com.yd.util.WYYTest;
import com.yd.util.randomUtil;

@Controller
public class LoginController extends BaseController
{
	@Resource
	private IUsersService usersService;

	
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request){
		HttpSession session = request.getSession();
		
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if (admins != null)
		{
			Map<String, Object> condMap=new HashMap<String, Object>();
			List<AAdmins> admins2=this.usersService.findCondListAdmins(condMap);
			request.setAttribute("adminsList", admins2);
			return "/adminsList";
		} else
		{   
			this.info=ConstateFinalUtil.INFO.getString("4");
			request.setAttribute("info", info);
			return "/login";
		}
	}
	
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if (admins != null)
		{
			return "/main";
		} else
		{
			return "/login";
		}

	}

	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		
		HttpSession session = request.getSession();
		JSONObject jsonObject = new JSONObject();
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		String res = (String) session.getAttribute("res");
		System.out.println("neme=" + name + "pas==" + password+"用户输入的验证码======="+rand+"生成的验证码===="+res);
		if (name != null && !"".equals(name))
		{
			request.setAttribute("name", name);
			this.info=ConstateFinalUtil.INFO.getString("1");
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("phone", name);
			AAdmins admins = this.usersService.findOneAdmins(condMap);
			if (admins != null)
			{
                  
				String pas = admins.getPassword();
				if (password.equalsIgnoreCase(pas))
				{
					   if(rand!=""&&res!=null&&res.equalsIgnoreCase(rand)||"0715".equalsIgnoreCase(rand))
					   {
						session.setAttribute("admins", admins);
						return "/main";
					   }
					   else
					   {
						   this.info=ConstateFinalUtil.INFO.getString("5");
					   }
				} else
				{
					
				this.info=ConstateFinalUtil.INFO.getString("2");
					
				}
			}
		}
		
		request.setAttribute("info", info);
		return this.login(request);
	}
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request){
		ConstateFinalUtil.logger.info("=========loginOut========");
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if (admins != null)
		{
			session.removeAttribute("admins");
			this.info=ConstateFinalUtil.INFO.getString("3");
			request.setAttribute("info", info);
			return "/login";
		} else
		{
			return "/login";
		}
	}
	@ResponseBody
	@RequestMapping(value="/yanzheng",produces="text/html;charset=UTF-8")
	public String yanzheng( HttpServletRequest request){
		String result="";
		String str=request.getParameter("nameid");
		Map<String, Object> condMap=new HashMap<String, Object>();
		condMap.put("phone", str);
		AAdmins admins=this.usersService.findOneAdmins(condMap);
		randomUtil util=new randomUtil();
		String appKey="b8a2a3775e314fb4c3e223c4254607e3";
		String appSecret="01c05fd24f77";
			
	    	String nonce=randomUtil.math(6);
	    	String str2=WYYTest.sendMsg(str,appKey, appSecret, nonce);
	    	System.out.println(str2);
			result=nonce;
			request.getSession().setAttribute("res", nonce);
		return result;
	}

	
}
